var app = getApp();
const apiHost = require('../../../common/api_host.js');
var utils = require('../../../utils/utils.js');
var httpJson=require('../../../utils/http_json.js');
Page({
  change_choose:function(e){
    // if(e.currentTarget.dataset.myid=='0'){
      this.setData({
        choose:e.currentTarget.dataset.myid
      })

  },
  getRoomList:function(pageNum){
    var  url=`${apiHost.config.portalApiHost}/portal/user/roomLike`;
    let data={pageNum,pageSize:10}
    httpJson.get(url,data,res=>{
      if(res.data.errorCode==0){
        setTimeout(()=>{
          wx.hideLoading()
          wx.stopPullDownRefresh()
        },1000)
        if(res.data.body==null||res.data.body.rows.length==0){
          this.setData({
            RoomNoContent:true
          })
          return;
        }

         /**start1套路代码 */
        if(pageNum==0){
          this.data.rooms=res.data.body.rows
        }
          /**判断是否还有数据 */
        if(res.data.body.totalPage==this.data.roomspageNum+1){
          this.setData({
            roomshasMore:false
          })
        }
          /**此时进行分页处理 */
        if(pageNum>0){
          this.data.rooms= this.data.rooms.concat(res.data.body.rows);
        }
        /**end1套路代码 */
       
        let rooms=res.data.body.rows;
      
        rooms.sort((p1,p2)=>{
          p1.createdTime-p2.createdTime
        })
        rooms.forEach((ele)=>{
          ele.mytime=utils.formatDate2(ele.createdTime)
        })
       
        this.setData({
          rooms:this.data.rooms,
          roomsTotalCount:res.data.body.totalCount
        })
      }
    })
  },
  getBrasinList:function(pageNum){
    var  url=`${apiHost.config.portalApiHost}/portal/user/brainLike`;
    let data={pageNum,pageSize:10}
    httpJson.get(url,data,res=>{
      if(res.data.errorCode==0){
        setTimeout(()=>{
          wx.hideLoading()
          wx.stopPullDownRefresh()
        },1000)
       
        if(res.data.body==null||res.data.body.rows.length==0){
          this.setData({
            BrasinNoContent:true
          })
          return;
        }
         /**start1套路代码 */
         if(pageNum==0){
          this.data.brains=res.data.body.rows
        }
          /**判断是否还有数据 */
        if(res.data.body.totalPage==this.data.brainspageNum+1){
          this.setData({
            brainshasMore:false
          })
        }
          /**此时进行分页处理 */
        if(pageNum>0){
          this.data.brains= this.data.brains.concat(res.data.body.rows);
        }
        /**end1套路代码 */
       
        let brains=res.data.body.rows;
       
        brains.sort((p1,p2)=>{
          p1.createdTime-p2.createdTime
        })
        brains.forEach((ele)=>{
          ele.mytime=utils.formatDate2(ele.createdTime)
        })
       
        this.setData({
          brains:this.data.brains,
          brainsTotalCount:res.data.body.totalCount
          })
         
        
       
      }
    })
  },


  addContentLike:function(e){
   
      let contentId=app.E(e,"myid");
      let userLike=app.E(e,'islike');
      let index=app.E(e,'idx');
      let mylist=app.E(e,'mylist');
      console.log(contentId,userLike,index)
      var  url;
      if(userLike){
        utils.successToast('取消惊叹成功',500)
        url =`${apiHost.config.portalApiHost}/portal/content/removeLike`;
        let data={contentId}
        httpJson.post(url,data,res=>{
          if(res.data.errorCode==0){
           
            var params={};
            if(mylist=='rooms'){
              var str=`rooms[${index}].userLike`;
              this.data.rooms[index].likeNum--
              this.setData({
                rooms:this.data.rooms
              })
            }
            if(mylist=='brains'){
              var str=`${mylist}[${index}].userLike`;
              this.data.brains[index].likeNum--
              this.setData({
                brains:this.data.brains
              })
            }
            params[str]=false;
           this.setData(params)
          }
        })
      }
      if(!userLike){
        utils.successToast('惊叹成功',500)
        url =`${apiHost.config.portalApiHost}/portal/content/addLike`;
        let data={contentId}
        httpJson.post(url,data,res=>{
          if(res.data.errorCode==0){
           
            var params={};
            if(this.data.choose==0){
              var str=`rooms[${index}].userLike`;
              this.data.rooms[index].likeNum++
             
              this.setData({
                rooms:this.data.rooms
              })
            }
            if(this.data.choose==1){
              var str=`brains[${index}].userLike`;
              this.data.brains[index].likeNum++
              this.setData({
                brains:this.data.brains
              })
            }
            params[str]=true;
           
           this.setData(params)
          
          }
        })
      }
    
  },

  /**
   * 页面的初始数据
   */
  data: {
    choose:'0',
    amazes:[
     
    ],
    roomspageNum:0,
    brainspageNum:0

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    this.getRoomList(0)
    this.getBrasinList(0)
   

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showLoading({

      title: '正在刷新',
    })
    if(this.data.choose==0){
      this.getRoomList(0);
      
      
    }
    if(this.data.choose==1){
      this.getBrasinList(0);
     
      
    }

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

         //地上发布
      if(this.data.choose==0){

        if(this.data.rooms.length<this.data.roomsTotalCount){
          this.setData({
            roomshasMore:true
          })
          
          let pageNum=++this.data.roomspageNum
          this.getRoomList(pageNum)
        }
    
      }
      //地下发布
      if(this.data.choose==1){
        
    
        if(this.data.brains.length<this.data.brainsTotalCount){
          
          this.setData({
            brainshasMore:true
          })
          let pageNum=++this.data.brainspageNum
          this.getBrasinList(pageNum)
        }
      }
        
    

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(res) {
    app.share()
    if (res.from === 'button') {
      var content=res.target.dataset.content
     
      if(content.length>50){
        content=content.substr(0,50)+'...'
      }
      var title=content
    }
    if(res.from === 'menu'){
      var imageUrl="/images/index/reply/reply.png";
      var title='我正在惊人院，邀请你来……'
    }
    return {
      title,
      imageUrl
      
    }
  }
})